﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BesAsm.MasterData.Entities;
using BesAsm.MasterData.Services;

namespace BesAsm.Emgaats.Tools
{
  public partial class UpdateLayerForm : Form
  {
    private LayerFile _layerFile;
    private Dictionary<string, string[]> _layerTypes;

    public UpdateLayerForm(LayerFile layerFile)
    {
      InitializeComponent();

      _layerFile = layerFile;
      txtLayerDescription.Text = _layerFile.Description;
      txtUploadedBy.Text = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
      txtName.Text = _layerFile.Name;

      _layerTypes = LayerFile.LayerTypes;

      string firstKey = _layerTypes.Keys.ElementAt(0);
      cmbCategories.DataSource = _layerTypes.Keys.ToList();
      cmbCategories.SelectedIndex = 0;
      cmbTables.DataSource = _layerTypes[firstKey];
      cmbTables.SelectedIndex = 0;
    }

    public LayerFile LayerFile
    {
      get { return _layerFile; }
    }

    private void btnOk_Click(object sender, EventArgs e)
    {
      try
      {
        _layerFile.Description = txtLayerDescription.Text;
        _layerFile.Name = txtName.Text;
        _layerFile.AppendedBy = txtUploadedBy.Text;
        _layerFile.AppendDate = DateTime.Now;
        _layerFile.Category = (string)cmbCategories.SelectedValue;
        _layerFile.AppliesToSchema = (string)cmbTables.SelectedValue;
      }
      catch (Exception ex)
      {
        MessageBox.Show("Error editing Layer File: " + ex.Message,
          "Error editing Layer File!", MessageBoxButtons.OK, MessageBoxIcon.Error);
      }
    }

    private void btnCancel_Click(object sender, EventArgs e)
    {
      _layerFile = null;
    }

    private void cmbCategories_SelectedIndexChanged(object sender, EventArgs e)
    {
      string key = _layerTypes.Keys.ElementAt(cmbCategories.SelectedIndex);      
      cmbTables.DataSource = _layerTypes[key];
      cmbTables.SelectedIndex = 0;
    }
  }
}
